<template>
  <div>
    <el-table :data="questionList" style="width: 100%" :header-cell-style="{background:'#FAFAFA'}">
      <el-table-column prop="number" label="考试编号" width="120"></el-table-column>
      <el-table-column prop="subject" label="学科"></el-table-column>
      <el-table-column prop="catalogID" label="目录"></el-table-column>
      <el-table-column prop="questionType" label="题型"></el-table-column>
      <el-table-column prop="question" label="题干" width="280"></el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180"></el-table-column>
      <el-table-column prop="difficulty" label="难度"></el-table-column>
      <el-table-column prop="creator" label="录入人"></el-table-column>
      <el-table-column prop="operation" label="操作" width="180">
        <!-- 操作模块利用插槽 -->
        <template slot-scope="scope">
          <!-- 预览 -->
          <el-button
            title="预览"
            icon="el-icon-view"
            type="primary"
            size="small "
            circle
            plain
            @click="$emit('preview',scope.$index,scope.row)"
          ></el-button>

          <!-- 修改 -->
          <el-button
            title="修改"
            icon="el-icon-edit"
            type="success"
            size="small "
            circle
            plain
            @click="handleEdit(scope.$index, scope.row)"
          ></el-button>

          <!-- 删除 -->
          <el-button
            title="删除"
            icon="el-icon-delete"
            type="danger"
            size="small "
            circle
            plain
            @click="$emit('delete',scope.row)"
          ></el-button>

          <!-- 加入精选 -->
          <el-button
            title="加入精选"
            icon="el-icon-check"
            type="warning"
            size="small "
            circle
            plain
            @click="join(scope.$index, scope.row)"
          ></el-button>
        </template>
        <!--/ 操作模块利用插槽 -->
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      class="pagination"
      background
      layout="prev,pager,next,sizes,jumper"
      :total="pages*num"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="num"
    ></el-pagination>
  </div>
</template>

<script>
export default {
  name: 'questionTable',
  props: {
    questionList: {
      type: Array,
      required: true
    },
    pages: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      num: 5
    }
  },

  methods: {
    handleEdit (index, row) {
      this.$router.push({ name: 'questions-new', query: { id: row.id } })
    },

    // 当每页要求展示的数据的数量发生变化时，触发
    handleSizeChange (val) {
      this.num = val
      this.$emit('sizeChange', val)
      // console.log(`每页 ${val} 条`);
    },

    // 当页码改变时，触发
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`);
      this.$emit('CurrentChange', val)
    },

    //点击加入精选时触发
    join (index, val) {
      console.log(val);
      this.$emit('joinClick', val)
    }

  }

}
</script>

<style scoped>
.pagination {
  float: right;
  margin-top: 20px;
  margin-bottom: 20px;
}

::v-deep .el-pagination__jump {
  margin-left: 0px;
}
</style>
